// pages/me/me.js
import {
  STATUS_BAR_HEIGHT,
  MENU_BUTTON_HEIGHT,
  NAVIGATION_BAR_HEIGHT,
  NICKNAME
} from '../../constant/constant'
import {
  getUserInfo
} from '../../api/main'
import {
  signOut
} from '../../api/login'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [{
        title: '个人信息'
      },
      {
        title: '积分排行'
      },
      {
        title: '分享文章'
      },
      {
        title: '分享项目'
      },
      {
        title: '收藏文章'
      }, {
        title: '退出登录'
      }
    ],
    statusBarHeight: wx.getStorageSync(STATUS_BAR_HEIGHT) + 'px',
    menuButtonHeight: wx.getStorageSync(MENU_BUTTON_HEIGHT) + 'px',
    navigationBarHeight: wx.getStorageSync(NAVIGATION_BAR_HEIGHT) + 'px',
    navigationBarAndStatusBarHeight: wx.getStorageSync(NAVIGATION_BAR_HEIGHT) + wx.getStorageSync(STATUS_BAR_HEIGHT) + 'px',
    hasLogin: app.globalData.hasLogined,
    nickname: wx.getStorageSync(NICKNAME),
    userInfo: {}

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo()
  },
  getUserInfo: function (showModal) {
    getUserInfo().then((res) => {
      if (res) {
        let data = res.data
        console.log('data', data)
        let coinInfo = data.coinInfo
        let userInfo = {}
        userInfo.coinCount = coinInfo.coinCount
        userInfo.level = coinInfo.level
        userInfo.rank = coinInfo.rank
        this.setData({
          userInfo: userInfo
        }, () => {
          if (showModal) {
            const {
              userInfo
            } = this.data
            wx.showModal({
              title: '积分排行',
              content: `当前积分${userInfo.coinCount}\r\n当前排行${userInfo.rank}`,
              confirmText: '确定',
              confirmColor: '#40e0d0',
              showCancel: false
            })
          }
        })
      }
    })
  },
  goLogin: function () {
    if (app.globalData.hasLogined) {
      wx.showToast({
        title: '已登录',
      })
    } else {
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },
  showCoinAndRank: function () {
    const {
      userInfo
    } = this.data
    if (userInfo) {
      wx.showModal({
        title: '积分排行',
        content: `当前积分${userInfo.coinCount}\r\n当前排行${userInfo.rank}`,
        confirmText: '确定',
        confirmColor: '#40e0d0',
        showCancel: false
      })
    } else {
      this.getUserInfo(true)
    }
  },
  signOut: function () {
    signOut().then((res) => {
      if (res) {
        this.setData({
          nickname: wx.getStorageSync(NICKNAME),
          hasLogin: app.globalData.hasLogined,
        })
      }
    })
  },
  showDetail: function (e) {
    if (this.data.hasLogin) {
      const {
        index,
      } = e.currentTarget.dataset
      switch (index) {
        case 0:
          wx.navigateTo({
            url: '../userinfo/userinfo',
          })
          break;
        case 1:
          this.showCoinAndRank()
          break;
        case 2:
          wx.navigateTo({
            url: '../shareArticle/shareArticle',
          })
          break;
        case 3:
          break;
        case 4:
          break;
        case 5:
          this.signOut()
          break
        default:
          break;
      }
    } else {
      wx.showModal({
        title: '登录提示',
        content: '请先登录',
        confirmText: '确定',
        cancelText: '取消',
        cancelColor: '#f6f6f6',
        confirmColor: '#40e0d0',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../login/login',
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3
      })
    }
    this.setData({
      nickname: wx.getStorageSync(NICKNAME),
      hasLogin: app.globalData.hasLogined,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})